﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using WPFHelpers;

namespace Tourbook
{
    public class ApplicationViewModel : ObservableObject
    {
        #region Members & ctors

        private IPageViewModel mCurrentPageViewModel;

        public ApplicationViewModel()
        {
            ChangePageCommand = new RelayCommand(
                      p => ChangeViewModel((IPageViewModel)p),
                      p => p is IPageViewModel);

            PageViewModels = new List<IPageViewModel>();
            PageViewModels.Add(new Tours.TourCollectionViewModel());
            PageViewModels.Add(new Riders.RiderCollectionViewModel());
            CurrentPageViewModel = PageViewModels[0];          
       }

        #endregion

        #region Properties / Commands

        public ICommand ChangePageCommand{ get; private set; }
        
        public List<IPageViewModel> PageViewModels{ get; private set; }            

        public IPageViewModel CurrentPageViewModel
        {
            get
            {
                return mCurrentPageViewModel;
            }
            set
            {
                if (mCurrentPageViewModel != value)
                {
                    mCurrentPageViewModel = value;
                    OnPropertyChanged("CurrentPageViewModel");
                }
            }
        }

        #endregion   

        #region Methods

        private void ChangeViewModel(IPageViewModel viewModel)
        {
            if (!PageViewModels.Contains(viewModel))
            {
                PageViewModels.Add(viewModel);
            }

            CurrentPageViewModel = PageViewModels.FirstOrDefault(vm => vm == viewModel);
        }

        #endregion      
    }
}
